const fs = require('fs');

let filePath = process.argv.slice(2)[0];
if (!filePath) filePath = "./diff";
const files = fs.readdirSync(filePath);
files.forEach(fileName => {
    fileName = filePath + '/' + fileName;
    fs.open(fileName, 'r', function (err, fd) {
        if (err) console.log(err);
        let size = fs.statSync(fileName).size - 1024;
        let buf = Buffer.alloc(size);
        fs.read(fd, buf, 0, size, 1024, function (err, bytesRead, buffer) {
            if (err) console.log(err);
            let wf = fileName;
            fs.open(wf, 'w+', function (err, fd) {
                if (err) console.log(err);
                fs.write(fd, buf, 0, buf.length, 0, function (err, bytesRead, buffer) {
                    if (err) console.log(err);
                    console.log(wf);
                    fs.close(fd, function (err) {
                        if (err) console.log(err);
                    })
                })
            })
        }) 
        fs.close(fd, function (err) {
            if (err) console.log(err);
        })
    });
})